<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>

      <div style="padding: 5px 10px">
        <h3>分解项目标</h3>
        <el-collapse v-model="activeNames">
          <el-collapse-item title="监测项目标信息" name="1">
            <template slot="title">
              <div class="card-header collapse-header" style='position: relative;overflow: hidden;'>
                <div class="head-title collapse-title">分解项目标信息</div>
                <div class="menu-title" style='position: absolute;right: 20px;top: -15px;' @click.stop='onHandleTarget'>
                  <i class="iconfont iconview icon-primary"></i>
                  <p class="menu-title-text">{{targetList.length == 0?'新增':'编辑' }}目标</p>
                </div>
              </div>
            </template>

            <table class="bs-table">
              <tr class="bs-table_row">
                <th class="bs-table_th">年度</th>
                <th class="bs-table_th">目标值</th>
                <th class="bs-table_th">目标内容</th>
              </tr>
              <tr class="bs-table_row" v-for="(li,k) in targetList" :key="k">
                <th class="bs-table_td">{{li.year}}年</th>
                <th class="bs-table_td">{{li.targetValue+li.targetValueUnit}}</th>
                <th class="bs-table_td">{{li.targetContent}}</th>
              </tr>
            </table>
            <div style="width:100%;text-align:center;padding-top:25px;font-size:20px;" v-if="targetList.length==0">
              暂无数据
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>

      <div style="padding: 5px 10px">
        <div style="display:flex;justify-content: space-between;">
          <h3>进度信息</h3>
          <div class="form-submit">
            <el-button v-if='!$route.query.watch' type="primary" @click.stop="report()">报送</el-button>
            <el-button type="info" @click.stop="goBack()">关闭</el-button>
            <el-button type="success" @click.stop="exportProgressFile()">导出进度信息</el-button>
          </div>
        </div>
        <el-tabs tab-position="top" v-if="$route.query.type=='1'">
          <el-tab-pane :label="item.year+'年'" v-for="item in viewSubmitFormList" :key="item.year">
            <div>
              <table class="bs-table">
                <tr class="bs-table_row">
                  <th class="bs-table_th" width="60" v-if="$route.query.submitPeriod=='1'">时间</th>
                  <th class="bs-table_th" width="120" v-if="$route.query.submitPeriod=='2'">时间</th>
                  <th class="bs-table_th" width="100" v-if="$route.query.submitPeriod=='3'">时间</th>
                  <th class="bs-table_th" width="160">地区名称</th>
                  <!--<th class="bs-table_th" width="120">地区代码</th>-->
                  <th class="bs-table_th" width="100">增加值</th>
                  <th class="bs-table_th" width="100">累计增加值</th>
                  <th class="bs-table_th" width="100">同比增速</th>
                  <th class="bs-table_th" width="100">累计同比增速</th>
                  <th class="bs-table_th" width="100">计量单位</th>
                  <th class="bs-table_th">备注</th>
                </tr>
                <tr class="bs-table_row" v-for="elem in item.submitList" v-if="item.submitList" :key="elem.year">
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='1'">{{elem.year}}年</th>
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='2'">{{elem.year}}年第{{elem.quarter}}季度</th>
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='3'">{{elem.year}}年{{elem.month}}月</th>
                  <th class="bs-table_td">
                    <el-button type="info" size="mini" @click="selectArea(elem.year,elem.quarter,elem.month,elem.period)"
                              plain>选择地区
                    </el-button>
                    <div style="font-size:14px;">
                      {{elem.areaName}}
                    </div>
                    <!--<el-input type="textarea" :rows="2"-->
                    <!--class="textarea" autosize-->
                    <!--placeholder="请输入地区名称" v-model="elem.areaName"></el-input>-->
                  </th>
                  <!--<th class="bs-table_td">-->
                  <!--{{elem.areaCode}}-->
                  <!--&lt;!&ndash;<el-input type="textarea" :rows="2"&ndash;&gt;-->
                  <!--&lt;!&ndash;class="textarea" autosize&ndash;&gt;-->
                  <!--&lt;!&ndash;placeholder="请输入地区代码" v-model="elem.areaCode"></el-input>&ndash;&gt;-->
                  <!--</th>-->
                  <th class="bs-table_td">
                    <el-input placeholder="输入值" v-model="elem.value"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-input placeholder="输入值" v-model="elem.valueLj"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-input placeholder="输入值" v-model="elem.tbzs"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-input placeholder="输入值" v-model="elem.tbzsLj"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-select v-model="elem.measurement" placeholder="单位">

                      <el-option
                        v-for="item in measurementDIC"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>

                    </el-select>
                    <!--<el-input placeholder="" v-model="elem.measurement"></el-input>-->
                  </th>
                  <th class="bs-table_td">
                    <el-input type="textarea" :rows="2"
                              class="textarea" autosize
                              placeholder="输入备注内容" v-model="elem.remark"></el-input>
                  </th>
                </tr>
              </table>
              <div class="d-f">
                  <span class='l-h-50 m-r-20 w-170'>规划进展情况:</span>
                  <el-upload
                    class="upload-demo"
                    ref="upload"
                    :limit="1"
                    :on-exceed="tipFileOne"
                    action="string"
                    :http-request="(params)=>onUploadFile(params,item,'ghjzjkfj')"
                    :file-list="!!fileInfo[item.year].ghjzjkfj?[{name: fileInfo[item.year].ghjzjkfj, id: fileInfo[item.year].ghjzjkfjId}]:[]"
                    :auto-upload="true"
                    :on-change="()=>{}"
                    :on-preview='onPreview'
                    >
                    <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </div>
                <div class="d-f">
                  <span class='l-h-50 m-r-20 w-170'>规划年度项目清单附件:</span>
                  <el-upload
                    class="upload-demo"
                    ref="upload"
                    :limit="1"
                    :on-exceed="tipFileOne"
                    action="string"
                    :http-request="(params)=>onUploadFile(params,item,'ghndxmqdfj')"
                    :file-list="!!fileInfo[item.year].ghndxmqdfj?[{name: fileInfo[item.year].ghndxmqdfj, id: fileInfo[item.year].ghndxmqdfjId}]:[]"
                    :auto-upload="true"
                    :on-change="()=>{}"
                    :on-preview="onPreview"
                    >
                    <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </div>
            </div>
          </el-tab-pane>
        </el-tabs>
        <el-tabs tab-position="top" v-else>
          <el-tab-pane :label="item.year+'年'" v-for="item in viewSubmitFormList" :key="item.year">
            <div>
              <table class="bs-table">
                <tr class="bs-table_row">
                  <th class="bs-table_th" width="60" v-if="$route.query.submitPeriod=='1'">时间</th>
                  <th class="bs-table_th" width="120" v-if="$route.query.submitPeriod=='2'">时间</th>
                  <th class="bs-table_th" width="100" v-if="$route.query.submitPeriod=='3'">时间</th>
                  <th class="bs-table_th" width="150">完成内容</th>
                  <th class="bs-table_th" width="150">有关说明</th>
                  <th class="bs-table_th" width="80">下步工作计划</th>
                </tr>
                <tr class="bs-table_row" v-for="elem in item.submitList" v-if="item.submitList" :key="elem.year">
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='1'">{{elem.year}}年</th>
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='2'">{{elem.year}}年第{{elem.quarter}}季度</th>
                  <th class="bs-table_td" v-if="$route.query.submitPeriod=='3'">{{elem.year}}年{{elem.month}}月</th>
                  <th class="bs-table_td">
                    <el-input type="textarea" :rows="2"
                              class="textarea" autosize
                              placeholder="请输入完成内容" v-model="elem.completeContent"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-input type="textarea" :rows="2"
                              class="textarea" autosize
                              placeholder="请输入有关说明" v-model="elem.havExplain"></el-input>
                  </th>
                  <th class="bs-table_td">
                    <el-input type="textarea" :rows="2"
                              class="textarea" autosize
                              placeholder="请输入下步工作计划" v-model="elem.nextStepPlan"></el-input>
                  </th>
                </tr>
              </table>
              <div class="d-f">
                  <span class='l-h-50 m-r-20 w-170'>规划进展情况:</span>
                  <el-upload
                    class="upload-demo"
                    ref="upload"
                    :limit="1"
                    :on-exceed="tipFileOne"
                    action="string"
                    :http-request="(params)=>onUploadFile(params,item,'ghjzjkfj')"
                    :file-list="!!fileInfo[item.year].ghjzjkfj?[{name: fileInfo[item.year].ghjzjkfj}]:[]"
                    :auto-upload="true"
                    :on-change="()=>{}"
                    >
                    <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </div>
                <div class="d-f">
                  <span class='l-h-50 m-r-20 w-170'>规划年度项目清单附件:</span>
                  <el-upload
                    class="upload-demo"
                    ref="upload"
                    :limit="1"
                    :on-exceed="tipFileOne"
                    action="string"
                    :http-request="(params)=>onUploadFile(params,item,'ghndxmqdfj')"
                    :file-list="!!fileInfo[item.year].ghndxmqdfj?[{name: fileInfo[item.year].ghndxmqdfj}]:[]"
                    :auto-upload="true"
                    :on-change="()=>{}"
                    >
                    <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!--<div class="form-submit">-->
      <!--<el-button v-if='!$route.query.watch' type="primary" @click.stop="report()">报送</el-button>-->
      <!--<el-button type="info" @click.stop="goBack()">关闭</el-button>-->
      <!--<el-button type="success" @click.stop="exportProgressFile()">导出进度信息</el-button>-->
      <!--</div>-->

      <div style="padding: 5px 10px">
        <h3>分解项信息</h3>
        <table class="bs-table">
          <tr class="bs-table_row">
            <th class="bs-table_th" style="width: 200px">规划名称</th>
            <td class="bs-table_td" colspan="3">{{detailList.planningTitle}}</td>
          </tr>
          <tr class="bs-table_row" v-if="detailList.type==1">
            <th class="bs-table_th">分解项名称</th>
            <td class="bs-table_td">{{detailList.itemName}}</td>
            <th class="bs-table_th">属性</th>
            <td class="bs-table_td"> {{detailList.attribute==0?'预期性':detailList.attribute==1?'约束性':''}}</td>
          </tr>
          <tr class="bs-table_row" v-else>
            <th class="bs-table_th">分解项名称</th>
            <td class="bs-table_td" colspan="3">{{detailList.itemName}}</td>
          </tr>

          <tr class="bs-table_row">
            <th class="bs-table_th" style="width: 200px">时间类型</th>
            <td class="bs-table_td">
              {{detailList.submitPeriod=='1'?'年报':detailList.submitPeriod=='2'?'季报':detailList.submitPeriod=='3'?'月报':''}}
            </td>
            <th class="bs-table_th">分解项类别</th>
            <td class="bs-table_td">
              {{detailList.type=='1'?'指标':detailList.type=='2'?'任务':detailList.type=='3'?'项目':''}}
            </td>
          </tr>
          <tr class="bs-table_row">
            <th class="bs-table_th">牵头部门</th>
            <td class="bs-table_td">{{detailList.leadDeptName}}</td>
            <th class="bs-table_th">配合部门</th>
            <td class="bs-table_td">{{detailList.cooDeptNameList|filter}}</td>
          </tr>
          <tr class="bs-table_row" v-if="detailList.type!=2">
            <th class="bs-table_th">起止日期</th>
            <td class="bs-table_td" v-if="detailList.startDate&detailList.endDate">
              {{detailList.startDate+'-'+detailList.endDate}}
            </td>
            <td class="bs-table_td" v-else>无</td>
            <th class="bs-table_th">总目标</th>
            <td class="bs-table_td">{{detailList.totalTarget+detailList.totalTargetUnit}}</td>
          </tr>


          <tr class="bs-table_row" v-if="detailList.type==1">
            <th class="bs-table_th">基数</th>
            <td class="bs-table_td">{{detailList.base}}</td>
            <th class="bs-table_th">年均增长</th>
            <td class="bs-table_td">{{detailList.yearAverageRise}}</td>
          </tr>
          <tr class="bs-table_row" v-if="detailList.type==1">
            <th class="bs-table_th">是否可量化</th>
            <td class="bs-table_td">{{detailList.quantifiable?'可量化':'不可量化'}}</td>
            <th class="bs-table_th">分类</th>
            <td class="bs-table_td">
              {{detailList.classify==1?'经济发展':detailList.classify==2?'创新驱动':detailList.classify==3?'民生福祉':detailList.classify==4?'资源环境':''}}
            </td>
          </tr>
          <tr class="bs-table_row" v-else-if="detailList.type==3">
            <th class="bs-table_th">是否可量化</th>
            <td class="bs-table_td" colspan="3">{{detailList.quantifiable?'可量化':'不可量化'}}</td>
          </tr>
          <tr class="bs-table_row">
            <th class="bs-table_th">分解项内容</th>
            <td class="bs-table_td" colspan="3">{{detailList.description}}</td>
          </tr>

        </table>
      </div>

      <!--增加指标弹出框begin-->
      <el-dialog :title="(targetList.length>0?'修改目标':'添加目标')"
                 :visible.sync="isShowTar" width="1000px" @close='isShowTar=false' @closed='isShowTar=false'>

        <div>
          <el-button type="primary" style="float:right" @click.stop="onHandleTarget();modifyTargetVisible=true">新增目标
          </el-button>
          <div slot="footer" class="dialog-footer">
            <avue-crud
              :data="tableData"
              :option="option"
              class="avuecrud"
            >
              <template slot-scope="scope" slot="menu">
                <div class="menu-title" v-if="!$route.query.watch" @click.stop="modifyTargetButton(scope.row)">
                  <i class="iconfont iconview icon-primary"></i>
                  <p class="menu-title-text">修改</p>
                </div>
                <div class="menu-title" v-if="!$route.query.watch" @click.stop="delTarget(scope.row,scope.index)">
                  <i class="iconfont iconview icon-primary"></i>
                  <p class="menu-title-text">删除</p>
                </div>
              </template>
            </avue-crud>
          </div>
        </div>
      </el-dialog>

      <!--增加指标弹出框begin-->
      <el-dialog :title="!!addId ? '修改目标' : '新增目标'" :visible.sync="modifyTargetVisible"
                 width="1000px" @closed="modifyTargetVisible=false">
        <el-form
          label-width="130px"
          :model="modifyTargetForm"
          style="padding-right: 30px;"
          :rules="modifyTargetRules"
          ref="modifyTargetForm"
        >
          <el-row>
            <el-col :span="18" style="margin-top: 27px;">
              <el-form-item class="form-item" label="年份 :" prop="year">
                <!--<el-input v-model="modifyTargetForm.year" placeholder="请输入年份"></el-input>-->
                <el-date-picker
                  v-model="modifyTargetForm.year"
                  type="year"
                  placeholder="请选择年份"
                  value-format="yyyy"
                  format="yyyy"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18" style="margin-top: 27px;">
              <el-form-item class="form-item" label="目标值 :" prop="targetValue">
                <el-input v-model="modifyTargetForm.targetValue" placeholder="请输入目标值"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18" style="margin-top: 27px;">
              <el-form-item class="form-item" label="目标值单位 :" prop="targetValueUnit">
                <el-input v-model="modifyTargetForm.targetValueUnit" placeholder="请输入目标值单位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18" style="margin-top: 27px;">
              <el-form-item class="form-item" label="目标内容 :" prop="targetContent">
                <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 4}"
                          v-model="modifyTargetForm.targetContent" placeholder="请输入目标内容"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="text-align:center">
            <el-col :span="18" style="width:100%">
              <el-button type="primary" @click="modifyWorkingItemTarget()">{{!!!addId?'新增':'修改'}}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>

      <!--选择地区-->
      <area-select :visible="areaSelectVisible" @getAreaInfo="getAreaInfo" @cancel="cancelGetArea"></area-select>
    </div>
  </basic-container>
</template>

<script>
  import { PROJECT_DIC } from "@/const/dict";
  import {pimWorkingItemRules} from "@/const/infoRules";
  import {
    decomposeItemDetail,
    deleteWorkingItemTarget,
    workingItemTargetList,
    workingItemTargetPage,
    submitTargetProgress,
    submitTaskProgress,
    submitProjectProgress,
    getOldTargetList,
    getOldTaskList,
    getOldProjectList,
    editTargetProgress,
    editTaskProgress,
    editProjectProgress,
    exportProgressFile,
    addWorkingItemTarget,
    modifyWorkingItemTarget,
  } from "@/api/pim/pim";
  import {mapGetters} from "vuex";
  import {filedownload,UploadFile} from "@/api/system";
  import {systemUrls} from '@/const/apiUrl'
  export default {
    name: "editProgress",
    created() {
      this.getDecomposeItemDetail();
      this.getWorkingItemTargetList();
      this.putSubmitFormList();
    },
    filters: {
      filter(e) {
        if (e) {
          let a = '';
          for (let li of e) {
            a = a + li
          }
          return a
        }
      }
    },
    computed: {
      ...mapGetters([
        "deptInfo"
      ])
    },
    data() {
      return {
        addId: '',
        isShowTar: false,
        modifyTargetVisible: false,
        tableData: [],
        modifyTargetForm: {},
         modifyTargetRules: {
          year: [
            {required: true, message: "请选择年份", trigger: "blur"}
          ],
          targetValue: [
            {required: true, message: "请输入目标值", trigger: "blur"}
          ],
          targetValueUnit: [
            {required: true, message: "请输入目标值单位", trigger: "blur"}
          ],
          targetContent: [
            {required: true, message: "请输入目标内容", trigger: "blur"}
          ],
        },
        measurementDIC: PROJECT_DIC.MEASUREMENT,
        headerData: {
          title: '进度信息'
        },
        activeNames: ['1'],
        detailList: {},
        targetList: [],
        targetForm: {    //指标表单要提交的字段
          id: null,
          ditemId: null,  //分解项ID
          period: this.$route.query.submitPeriod,   //周期类型
          year: null,
          month: null,
          quarter: null,
          areaCode: null,
          areaName: null,
          value: null,
          valueLj: null,
          tbzs: null,
          tbzsLj: null,
          measurement: null,
          remark: null,
        },
        taskForm: {    //任务和项目表单要提交的字段
          id: null,
          ditemId: null,  //分解项ID
          period: this.$route.query.submitPeriod,   //周期类型
          year: null,
          month: null,
          quarter: null,
          completeContent: null,
          havExplain: null,
          nextStepPlan: null,
        },
        submitFormList: [],//提交的表单数组
        viewSubmitFormList: [],//展示用的表单数组
        areaSelectVisible: false,
        yearTemp: '',
        quarterTemp: '',
        monthTemp: '',
        periodTemp: '',
        fileInfo: {},
        option: {
          page: true,
          addBtn: false,
          editBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          menuWidth: 150,
          keyId: "id",
          column: [{
            label: "年份",
            prop: "year",
            cell: true
          },
            {
              label: "目标值",
              prop: "targetValue",
              cell: true
            },
            {
              label: "目标值单位",
              prop: "targetValueUnit",
              cell: true
            },
            {
              label: "目标内容",
              width: 400,
              prop: "targetContent",
              cell: true
            },
          ]
        },

      }
    },
    methods: {
      onPreview(file) {
        window.location.href=`${systemUrls.downloadUrl}/${file.id}`;
      },
      onHandleTarget() {
        this.addId='';
        this.getWorkingItemTargetList();
        this.isShowTar=true;
        this.modifyTargetForm = null;
        this.modifyTargetForm = {};
      },
      delTarget(row) {
        this.$confirm("是否删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteWorkingItemTarget(row.id).then(response => {
            if (response.data.code == 0) {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
                duration: 2000
              });
              this.getWorkingItemTargetList(this.ditemId)
            }
          })
        })
      },
      modifyTargetButton(row) {
        console.log(row);
        this.addId = row.id;
        this.modifyTargetForm = {...row};
        this.modifyTargetVisible = true
      },
      modifyWorkingItemTarget() {
        let n = this.modifyTargetForm;
        let data = {
          year: n.year,
          targetContent: n.targetContent,
          targetValue: n.targetValue,
          targetValueUnit: n.targetValueUnit,
          // ditemId: this.addId.toString(),
        };
        this.$refs.modifyTargetForm.validate(valid => {
          if (valid) {
            this.$confirm(this.addId ? "确定新增目标吗?" : "确定修改目标吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              if (!!!this.addId) {
                addWorkingItemTarget({
                  ...data,
                  ditemId: this.$route.query.id
                }).then(response => {
                  if (response.data.code == 0) {
                    this.$notify({
                      title: "成功",
                      message: "新增成功",
                      type: "success",
                      duration: 2000
                    });
                    this.modifyTargetVisible = false;
                    this.getWorkingItemTargetList()
                  }
                })
              } else {
                modifyWorkingItemTarget(this.addId, data).then(response => {
                  if (response.data.code == 0) {
                    this.$notify({
                      title: "成功",
                      message: "修改成功",
                      type: "success",
                      duration: 2000
                    });
                    this.modifyTargetVisible = false;
                    this.getWorkingItemTargetList()
                  }
                })
              }

            })
          }
        })
      },
      getDecomposeItemDetail() {
        decomposeItemDetail(this.$route.query.id).then(response => {
          if (response.data.code == 0) {
            this.detailList = response.data.data
          }
        })
      },
      getWorkingItemTargetList() {
        workingItemTargetList(this.$route.query.id).then(response => {
          if (response.data.code == 0) {
            this.targetList = response.data.data
            console.log()
          }
        });

        workingItemTargetPage({ditemId: this.$route.query.id}).then(response=>{
          if (response.data.code == 0) {
            for (let i of response.data.data.records) {
              i.year = i.year.toString()
            }
            console.warn(response.data)
            this.tableData = response.data.data.records
          }
        })

      },
      report() {
        this.$confirm("确定报送吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          let formList = await this.handleFormList();
          if (this.$route.query.type == '1') { //指标
            let submitForm = Object.assign({
              pimProgressTargetDTOList: [],
            });
            submitForm.pimProgressTargetDTOList = formList.map(i=>{
              return {
                ...i,
                ghjzjkfj: this.fileInfo[i.year].ghjzjkfj||i.ghjzjkfj,
                ghjzjkfjId: this.fileInfo[i.year].ghjzjkfjId||i.ghjzjkfjId,
                ghndxmqdfj: this.fileInfo[i.year].ghndxmqdfj||i.ghndxmqdfj,
                ghndxmqdfjId: this.fileInfo[i.year].ghndxmqdfjId||i.ghndxmqdfjId,
              }
            });
            editTargetProgress(submitForm).then(response => {
              if (response.data.code == '0') {
                this.$message({
                  message: '报送成功！',
                  type: 'success'
                });
                this.goBack();
              }
            })
          } else if (this.$route.query.type == '2') { //任务
            let submitForm = Object.assign({
              pimProgressTaskDTOList: [],
            });

            // var flag = formList.every(function(item){
            //   return (item.completeContent  != null && item.completeContent != "");
            // })
            // if(flag === false){ // flag === true  (formList下面的completeContent都不为空)
            //   this.$notify({
            //     title: "提醒",
            //     type: "warning",
            //     message: "完成内容未填写",
            //     duration: 2000
            //   });
            //   return false
            // }

            // submitForm.pimProgressTaskDTOList = formList;
            submitForm.pimProgressTaskDTOList = formList.map(i=>{
              return {
                ...i,
                ghjzjkfj: this.fileInfo[i.year].ghjzjkfj||i.ghjzjkfj,
                ghjzjkfjId: this.fileInfo[i.year].ghjzjkfjId||i.ghjzjkfjId,
                ghndxmqdfj: this.fileInfo[i.year].ghndxmqdfj||i.ghndxmqdfj,
                ghndxmqdfjId: this.fileInfo[i.year].ghndxmqdfjId||i.ghndxmqdfjId,
              }
            });
            editTaskProgress(submitForm).then(response => {
              if (response.data.code == '0') {
                this.$message({
                  message: '报送成功！',
                  type: 'success'
                });
                this.goBack();
              }
            })
          } else if (this.$route.query.type == '3') { //项目
            let submitForm = Object.assign({
              pimProgressProjectDTOList: [],
            });


            // var flag = formList.every(function(item){
            //   return (item.completeContent  != null && item.completeContent != "");
            // })
            // console.log(flag)
            // if(flag === false){ // flag === true  (formList下面的completeContent都不为空)
            //   console.log("为空")
            //   this.$notify({
            //     title: "提醒",
            //     type: "warning",
            //     message: "完成内容未填写",
            //     duration: 2000
            //   });
            //   return false
            // }


            submitForm.pimProgressProjectDTOList = formList.map(i=>{
              return {
                ...i,
                ghjzjkfj: this.fileInfo[i.year].ghjzjkfj||i.ghjzjkfj,
                ghjzjkfjId: this.fileInfo[i.year].ghjzjkfjId||i.ghjzjkfjId,
                ghndxmqdfj: this.fileInfo[i.year].ghndxmqdfj||i.ghndxmqdfj,
                ghndxmqdfjId: this.fileInfo[i.year].ghndxmqdfjId||i.ghndxmqdfjId,
              }
            });;
            
            editProjectProgress(submitForm).then(response => {
              if (response.data.code == '0') {
                this.$message({
                  message: '报送成功！',
                  type: 'success'
                });
                this.goBack();
              }
            })
          }

        })
      },
      handleFormList() {
        if (this.$route.query.submitPeriod == '1') {  //年报
          for (let submitItem in this.submitFormList) {
            for (let viewItem in this.viewSubmitFormList) {
              if (this.submitFormList[submitItem].year == this.viewSubmitFormList[viewItem].year) {
                this.submitFormList[submitItem] = this.deepClone(this.viewSubmitFormList[viewItem].submitList[0]);
                break;
              }
            }
          }
          return this.submitFormList;
        } else if (this.$route.query.submitPeriod == '2') { //季报
          for (let submitItem in this.submitFormList) {
            for (let viewItem in this.viewSubmitFormList) {
              if (this.submitFormList[submitItem].year == this.viewSubmitFormList[viewItem].year) {
                for (let item in this.viewSubmitFormList[viewItem].submitList) {
                  if (this.viewSubmitFormList[viewItem].submitList[item].quarter == this.submitFormList[submitItem].quarter) {
                    this.submitFormList[submitItem] = this.deepClone(this.viewSubmitFormList[viewItem].submitList[item]);
                    break;
                  }
                }
              }
            }
          }
          return this.submitFormList;
        } else if (this.$route.query.submitPeriod == '3') { //月报
          for (let submitItem in this.submitFormList) {
            for (let viewItem in this.viewSubmitFormList) {
              if (this.submitFormList[submitItem].year == this.viewSubmitFormList[viewItem].year) {
                for (let item in this.viewSubmitFormList[viewItem].submitList) {
                  if (this.viewSubmitFormList[viewItem].submitList[item].month == this.submitFormList[submitItem].month) {
                    this.submitFormList[submitItem] = this.deepClone(this.viewSubmitFormList[viewItem].submitList[item]);
                    break;
                  }
                }
              }
            }
          }
          return this.submitFormList;
        }

      },
      goBack() {
        this.$router.go(-1);
      },
      async putSubmitFormList() {
        let startDateInt = Number(this.$route.query.startDate);
        let endDateInt = Number(this.$route.query.endDate);
        const fileInfo = {
          ghjzjkfj: '',
          ghjzjkfjId: '',
          ghndxmqdfj: '',
          ghndxmqdfjId: '',
        };

        for (let item = startDateInt; item <= endDateInt; item++) {
          this.fileInfo[item]=fileInfo;
        }

        if (this.$route.query.type == '1') { //指标要提交的表单
          let records = await this.getOldTargetListYear();

          if (this.$route.query.submitPeriod == '1') { //年报
            for (let item = startDateInt; item <= endDateInt; item++) {

              //提交表单的处理
              let form = this.deepClone(this.targetForm);
              form.ditemId = this.$route.query.id;
              form.year = item.toString();
              this.submitFormList.push(form);
              //表单显示的处理
              let viewSubmit = Object.assign({
                year: item.toString(),
                submitList: []
              });

              for (let recordsItem in records) {
                if (records[recordsItem].year == item.toString()) {
                  console.log(recordsItem,records[recordsItem])
                  viewSubmit.submitList.push(records[recordsItem]);
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          } else if (this.$route.query.submitPeriod == '2') {//季报
            for (let item = startDateInt; item <= endDateInt; item++) {
              let viewSubmit = Object.assign({
                year: '',
                submitList: []
              });
              for (let quarter = 1; quarter <= 4; quarter++) {//季度循环
                let form = this.deepClone(this.targetForm);
                form.ditemId = this.$route.query.id;
                form.year = item.toString();
                form.quarter = quarter.toString();
                this.submitFormList.push(form);

                viewSubmit.year = item.toString();
                for (let recordsItem in records) {
                  if (records[recordsItem].year == item.toString() && records[recordsItem].quarter == quarter.toString()) {
                    viewSubmit.submitList.push(records[recordsItem]);
                  }
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          } else if (this.$route.query.submitPeriod == '3') {//月报
            for (let item = startDateInt; item <= endDateInt; item++) {
              let viewSubmit = Object.assign({
                year: '',
                submitList: []
              });
              for (let month = 1; month <= 12; month++) {//月份循环
                let form = this.deepClone(this.targetForm);
                form.ditemId = this.$route.query.id;
                form.year = item.toString();
                form.month = month;
                this.submitFormList.push(form);

                viewSubmit.year = item.toString();
                for (let recordsItem in records) {
                  if (records[recordsItem].year == item.toString() && records[recordsItem].month == month) {
                    viewSubmit.submitList.push(records[recordsItem]);
                  }
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          }
        } else if (this.$route.query.type == '2' || this.$route.query.type == '3') {  //任务或项目要提交的表单
          let records = [];
          if (this.$route.query.type == '2') {
            records = await this.getOldTaskListYear();
          } else if (this.$route.query.type == '3') {
            records = await this.getOldProjectListYear();
          }

          if (this.$route.query.submitPeriod == '1') { //年报
            for (let item = startDateInt; item <= endDateInt; item++) {
              let form = this.deepClone(this.taskForm);
              form.ditemId = this.$route.query.id;
              form.year = item.toString();
              this.submitFormList.push(form);
              //放入list中便于显示的遍历
              let viewSubmit = Object.assign({
                year: item.toString(),
                submitList: []
              });
              for (let recordsItem in records) {
                if (records[recordsItem].year == item.toString()) {
                  viewSubmit.submitList.push(records[recordsItem]);
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          } else if (this.$route.query.submitPeriod == '2') {//季报
            for (let item = startDateInt; item <= endDateInt; item++) {
              let viewSubmit = Object.assign({
                year: '',
                submitList: []
              });
              for (let quarter = 1; quarter <= 4; quarter++) {//季度循环
                let form = this.deepClone(this.taskForm);
                form.ditemId = this.$route.query.id;
                form.year = item.toString();
                form.quarter = quarter.toString();
                this.submitFormList.push(form);

                viewSubmit.year = item.toString();
                for (let recordsItem in records) {
                  if (records[recordsItem].year == item.toString() && records[recordsItem].quarter == quarter.toString()) {
                    viewSubmit.submitList.push(records[recordsItem]);
                  }
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          } else if (this.$route.query.submitPeriod == '3') {//月报
            for (let item = startDateInt; item <= endDateInt; item++) {
              let viewSubmit = Object.assign({
                year: '',
                submitList: []
              });
              for (let month = 1; month <= 12; month++) {//月份循环
                let form = this.deepClone(this.taskForm);
                form.ditemId = this.$route.query.id;
                form.year = item.toString();
                form.month = month;
                this.submitFormList.push(form);

                viewSubmit.year = item.toString();
                for (let recordsItem in records) {
                  if (records[recordsItem].year == item.toString() && records[recordsItem].month == month) {
                    viewSubmit.submitList.push(records[recordsItem]);
                  }
                }
              }
              this.viewSubmitFormList.push(viewSubmit);
            }
          }
        }

        this.viewSubmitFormList.forEach((formInfo)=> {
          try {
            this.fileInfo[formInfo.year] = {
              ...this.fileInfo[formInfo.year],
              ghjzjkfj: formInfo.submitList[0].ghjzjkfj||'',
              ghjzjkfjId: formInfo.submitList[0].ghjzjkfjId||'',
              ghndxmqdfj: formInfo.submitList[0].ghndxmqdfj||'',
              ghndxmqdfjId: formInfo.submitList[0].ghndxmqdfjId||'',
            }
          } catch (error) {
            console.log(11111111111)

            console.error(error)
          }
        })
      },
      async getOldTargetListYear() {  //指标的表单数据
        return new Promise((resolve, reject) => {
          getOldTargetList(this.$route.query.id).then(response => {
            if (response.data.code == 0) {
              resolve(response.data.data)
            }
          })
        })

      },
      async getOldTaskListYear() {  //任务的表单数据
        return new Promise((resolve, reject) => {
          getOldTaskList(this.$route.query.id).then(response => {
            if (response.data.code == 0) {
              resolve(response.data.data)
            }
          })
        })

      },
      async getOldProjectListYear() {  //项目的表单数据
        return new Promise((resolve, reject) => {
          getOldProjectList(this.$route.query.id).then(response => {
            if (response.data.code == 0) {
              resolve(response.data.data)
            }
          })
        })

      },

      selectArea(year, quarter, month, period) {
        this.yearTemp = year;
        this.quarterTemp = quarter;
        this.monthTemp = month;
        this.periodTemp = period;

        this.areaSelectVisible = true;
      },
      getAreaInfo(fullName, areaCode) {
        this.dealViewSubmitFormList(fullName, areaCode);
        this.areaSelectVisible = false;
      },
      cancelGetArea() {
        this.areaSelectVisible = false;
      },
      dealViewSubmitFormList(fullName, areaCode) {
        for (let viewItem in this.viewSubmitFormList) {
          if (this.viewSubmitFormList[viewItem].year == this.yearTemp) {
            if (this.periodTemp=='1') {  //年报
              this.viewSubmitFormList[viewItem].submitList[0].areaName = fullName;
              this.viewSubmitFormList[viewItem].submitList[0].areaCode = areaCode;
            }else if(this.periodTemp=='2'){  //季报
              for (let submitItem in this.viewSubmitFormList[viewItem].submitList) {
                if(this.viewSubmitFormList[viewItem].submitList[submitItem].quarter == this.quarterTemp){
                  this.viewSubmitFormList[viewItem].submitList[submitItem].areaName = fullName;
                  this.viewSubmitFormList[viewItem].submitList[submitItem].areaCode = areaCode;
                }
              }
            }else if(this.periodTemp=='3'){  //月报
              for (let submitItem in this.viewSubmitFormList[viewItem].submitList) {
                if(this.viewSubmitFormList[viewItem].submitList[submitItem].month == this.monthTemp){
                  this.viewSubmitFormList[viewItem].submitList[submitItem].areaName = fullName;
                  this.viewSubmitFormList[viewItem].submitList[submitItem].areaCode = areaCode;
                }
              }
            }

          }
        }
      },
      exportProgressFile(){
        this.$confirm("确定要导出进度信息吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportProgressFile(this.$route.query.id).then(response => {
            let exportFileName = "关于"+this.$route.query.itemName+"分解项的进度信息.docx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },

      onUploadFile(param, item, type) {
          const formData = new FormData();
          formData.append(param.file.uid, param.file);
          UploadFile(formData,{fileType:'2'}).then(response => {
            if (response.data.code == 0) {
              
              this.fileInfo[item.year]={
                ...this.fileInfo[item.year],
                [type]:param.file.name,
                [`${type}Id`]:response.data.data
              }
              this.$notify({
                title: "成功",
                message: "上传成功",
                type: "success",
                duration: 2000
              });
            }
          }).catch(response => {
            // this.fileList = [];
            this.$notify({
              title: "失败",
              message: "上传失败",
              type: "error",
              duration: 2000
            });
            // reject()
          })
        // })
        
      },

    }
  }
</script>

<style lang="scss" scoped>
  /deep/ .textarea textarea {
    height: 80px !important;
  }

  /deep/ .upload-demo {
    padding-top: 10px
  }

  .relateddiv {
    margin-left: 0
  }

  .time {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }

  .bs-table_td {
    font-size: 16px;
    padding: 4px;
  }

  /deep/.el-input__inner {
    /*border-bottom-style: 1px solid red;*/
    border-right-style: none;
    border-top-style: none;
    border-left-style: none;
    /*border-bottom-style: 1px solid red;*/
    background-color: #FFF;
    background-image: none;
    border-radius: 0;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: 0;
    padding: 0 15px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
  }
  /deep/.el-textarea__inner {
    border:none;
    display: block;
    resize: vertical;
    padding: 5px 15px;
    line-height: 1.5;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    font-size: inherit;
    color: #606266;
    background-color: #FFF;
    background-image: none;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
  }
  /deep/.el-button--info.is-plain {
    color: #ffffff;
    background: #2077be;
    border-color: #2077be;
    margin-bottom: 5px;
  }
</style>
